package com.shanzmoo.platform.manage.api.manager.product;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.util.CollectionUtil;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.db.module.product.domain.dto.ScoreSaveDto;
import com.shanzmoo.db.module.product.domain.dto.ScoreUpdateDto;
import com.shanzmoo.db.module.product.domain.query.ScoreQueryVO;
import com.shanzmoo.db.module.product.domain.vo.BrandVO;
import com.shanzmoo.db.module.product.domain.vo.CategoryVO;
import com.shanzmoo.db.module.product.domain.vo.ProductScoreVO;
import com.shanzmoo.db.module.product.domain.vo.SkuResultVO;
import com.shanzmoo.db.module.product.entity.ProductBrandEntity;
import com.shanzmoo.db.module.product.entity.ProductScoreEntity;
import com.shanzmoo.db.module.product.entity.ProductSkuEntity;
import com.shanzmoo.db.module.product.entity.ProductSpuEntity;
import com.shanzmoo.db.module.product.service.IProductScoreService;
import com.shanzmoo.db.module.product.service.IProductSkuService;
import com.shanzmoo.db.module.product.service.IProductSpuService;
import com.shanzmoo.platform.manage.api.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 商品分类manager
 * @author xueqz
 */
@Component
public class ProductScoreManager {

    @Autowired
    private  IProductScoreService productScoreService;

    @Autowired
    private IProductSkuService productSkuService;

    public SHttpResponse<PageRespBase<ProductScoreVO>> getProductScoreList(ScoreQueryVO scoreQueryVO) {
        // 开启分页
        IPage<ProductScoreVO> productScoreVOIPage = productScoreService.getProductScoreList(scoreQueryVO);
        PageRespBase<ProductScoreVO> re = PageRespBase.create(productScoreVOIPage, productScoreVOIPage.getRecords());
        return SHttpResponse.ok(re);
    }

    public SHttpResponse<Boolean> addScore(ScoreSaveDto scoreSaveDto) {
        //校验是否只有一个sku
        ProductSkuEntity skuEntity = productSkuService.getById(scoreSaveDto.getSku());
        List<ProductSkuEntity> list = productSkuService.list(new QueryWrapper<ProductSkuEntity>().eq("deleted", Boolean.FALSE).eq("parent_id", skuEntity.getParentId()));
        if(CollUtil.isNotEmpty(list) && list.size() != 1)throw new RuntimeException("仅只有单sku的spu才可配置为积分商品");
        List<ProductScoreEntity> list2 = productScoreService.list(new QueryWrapper<ProductScoreEntity>().eq("deleted", Boolean.FALSE).eq("sku", scoreSaveDto.getSku()));
        if(CollUtil.isNotEmpty(list2))throw new RuntimeException("不可重复添加未失效的sku为积分商品");
        ProductScoreEntity convert = scoreSaveDto.convert(ProductScoreEntity.class);
        convert.setCreateUser(SecurityUtil.getCurrentUserId());
        productScoreService.save(convert);
        return SHttpResponse.ok();
    }


    public SHttpResponse<Boolean> updateScore(ScoreUpdateDto scoreUpdateDto) {
        ProductScoreEntity convert = scoreUpdateDto.convert(ProductScoreEntity.class);
        convert.setCreateUser(SecurityUtil.getCurrentUserId());
        productScoreService.updateById(convert);
        return SHttpResponse.ok();
    }

    public SHttpResponse<Boolean> operationScore(Integer id, Boolean shelvesMark) {
        ProductScoreEntity productScoreEntity = new ProductScoreEntity();
        productScoreEntity.setId(id);
        productScoreEntity.setShelves(shelvesMark);
        productScoreEntity.setUpdateUser(SecurityUtil.getCurrentUserId());
        productScoreService.updateById(productScoreEntity);
        return SHttpResponse.ok();
    }
}
